package com.anlu.pattern.create.singleton;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author anlu
 * @create 2019/11/21
 **/

public class SessionCount {

    private AtomicInteger count = new AtomicInteger(0);

    private static SessionCount instance;//懒汉式

    /**
     * 私有的构造方法
     */
    private SessionCount(){}
    //获取唯一可用的对象
    public static SessionCount getInstance(){
        if(instance==null){
            synchronized (SessionCount.class){
                if(instance==null){
                    instance = new SessionCount();
                }
            }
        }
        return instance;
    }

    /***以下是业务方法***/
    public int plus(){
        return count.incrementAndGet();
    }

    public int decrease(){
        return count.decrementAndGet();
    }

    public void showMessage(){
        System.out.println("当前人数："+this.count.get());
    }

}
